const { User } = require('../utils/mongoUtil')

const saveUser = ({ username, password }) => {
    // 返回Promise
    return new User({username, password}).save()
}

const findUser = (username) => {
    return User.where({username}).findOne()
}

const findUserList = () => {
    // 倒序获取所有的用户, 不获取密码
    return User.find({}, {_id: 1, username: 1}).sort({_id: -1})
}

const deleteUserById = (id) => {
    return User.deleteOne({_id: id})
}

module.exports = {
    saveUser, findUser, findUserList, deleteUserById
}

